
package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;


public class ReturnGUI extends JPanel implements ActionListener
{
	private Box yBox1, xBox2;
	private JPanel tablePanel = null;
	private DefaultTableModel returnModel = null;
	private JTable returnTable = null;
	private JCheckBox damageCheckBox = null;
	
	JButton refundB, cancelB, confirmB;
	
	StockItemDAO dao = null;
	
	private JFrame parentFrame = null;
	private MainGUI mainGUI = null;
	

	public ReturnGUI(JFrame parentFrame, MainGUI creator) 
	{
		mainGUI = creator;
		this.parentFrame = parentFrame;
		//Create data access object
		dao = new StockItemDAO();
		//Set name of this panel
		this.setName("ReturnGUI");
		//table Panel
		tablePanel = new JPanel(new GridLayout(1,1));
		tablePanel.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
		JPanel returnTableP = new JPanel(new GridLayout(1,1));
		returnModel = new DefaultTableModel();
		returnTable = new JTable(returnModel);
		returnModel.addColumn("stockId");
		returnModel.addColumn("title");
		returnModel.addColumn("status");
		returnModel.addColumn("dateLastRented");
		returnTableP.setPreferredSize(new Dimension(200,150));
		JScrollPane returnJSP = new JScrollPane(returnTable);
		returnTableP.add(returnJSP);
		tablePanel.add(returnTableP);
		
		damageCheckBox = new JCheckBox("Damaged");
		//JTextArea jta2 = new JTextArea();
		//jta2.setText("KEY");
		//jta2.setBorder(BorderFactory.createCompoundBorder(
		//				BorderFactory.createEtchedBorder(),
		//				BorderFactory.createLoweredBevelBorder()));
		//jta2.setPreferredSize(new Dimension(50,70));
		//xBox1.add(jta2);
		//xBox2
		xBox2 = new Box(BoxLayout.X_AXIS);
		refundB = new JButton("REFUND");
		refundB.addActionListener(this);
		xBox2.add(refundB);
		cancelB = new JButton("CLEAR");
		cancelB.addActionListener(this);
		xBox2.add(cancelB);
		confirmB = new JButton("CONFIRM");
		confirmB.addActionListener(this);
		xBox2.add(confirmB);	
		xBox2.add(damageCheckBox);	
		
		//this panel
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JLabel returnL = new JLabel("RETURN", JLabel.CENTER);
		//returnL.setPreferredSize(new Dimension(Short.MAX_VALUE,25));
		returnL.setOpaque(true);
		returnL.setBackground(Color.BLACK);
		returnL.setForeground(Color.WHITE);
		JPanel returnP = new JPanel(new GridLayout(1,1));
		returnP.setPreferredSize(new Dimension(Short.MAX_VALUE,25));
		returnP.add(returnL);
		this.add(returnP);
		this.add(tablePanel);
		this.add(xBox2);
		this.setPreferredSize(new Dimension(Short.MAX_VALUE, 250));
	}//end constructor
	
	public void updateReturnTable(Object [] row)
	{
		int i = 0;
		boolean duplicateFlag = false;
		while(i < returnTable.getRowCount())
		{
			System.out.println("In while...");
			if(row[0].equals(returnTable.getValueAt(i,0)))
			{
				JOptionPane.showMessageDialog(this, "You may not add the same stock " +
					"\nitem more than once.", "DUPLICATE ENTRY", JOptionPane.WARNING_MESSAGE);
				duplicateFlag = true;
			}
			i++;
		}	
		if(duplicateFlag == false)
		{
			returnModel.addRow(row);
		}
	}
	
	public void removeRows()
	{
		int rowCount = returnTable.getRowCount();
			for(int i = 0; i < rowCount; i++)
			{
				returnModel.removeRow(0);
			}
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if(source == cancelB)
		{
			removeRows();
		}
		if(source == confirmB)
		{
			if(returnTable.getRowCount() < 1)
			{
				JOptionPane.showMessageDialog(this, " No items to return ");
			}
			else if(returnTable.getRowCount() > 0)
			{
				PasswordXXX passDialog = new PasswordXXX(parentFrame, 
					"     Enter employee password...", dao.getConnection());
				if(passDialog.getLogonSuccess() == true)
				{
					if(damageCheckBox.isSelected())
					{
						Vector stockIdVect = new Vector();
						for(int i = 0; i < returnTable.getRowCount(); i++)
						{
							stockIdVect.addElement(returnTable.getValueAt(i,0));
						}
						dao.confirmReturn(0.0, "R", passDialog.getEmpId(), stockIdVect, "DAMAGED");
						removeRows();
						mainGUI.clickSearch();
						damageCheckBox.setSelected(false);
						JOptionPane.showMessageDialog(this, " Item returned. ");
					}
					else
					{
						Vector stockIdVect = new Vector();
						for(int i = 0; i < returnTable.getRowCount(); i++)
						{
							stockIdVect.addElement(returnTable.getValueAt(i,0));
						}
						dao.confirmReturn(0.0, "R", passDialog.getEmpId(), stockIdVect, "IN");
						removeRows();
						mainGUI.clickSearch();
						JOptionPane.showMessageDialog(this, " Item returned. ");
					}
				}
			
			}
			
			
		}
		if(source == refundB)
		{
			if(returnTable.getRowCount() == 1)
			{
				PasswordXXX passDialog = new PasswordXXX(parentFrame, 
					"     Enter employee password...", dao.getConnection());
				if(passDialog.getLogonSuccess() == true)
				{
					String stockId = (String)returnTable.getValueAt(0,0);
					System.out.println("Refund - StockId = " + stockId);
					if(damageCheckBox.isSelected())
					{
						dao.refund(stockId, "DAMAGED", passDialog.getEmpId());
						JOptionPane.showMessageDialog(this,
							" Item marked \"DAMAGED\" and customer credited. ");
					}
					else
					{
						dao.refund(stockId, "IN", passDialog.getEmpId());
						JOptionPane.showMessageDialog(this,
							" Item returned and customer credited. ");
					}
					cancelB.doClick();
					mainGUI.clickSearch();
				}
				
			}
			else if(returnTable.getRowCount() < 1)
			{
				JOptionPane.showMessageDialog(this, " No item selected. ");
			}
			else if(returnTable.getRowCount() > 1)
			{
				JOptionPane.showMessageDialog(this, " You may only perform a refund\n " +
													" for one item at a time. ");
			}
		}
	}
}
